using IQIGame.Onigao.Framework;
using NetProtocol.POD;

namespace IQIGame.Onigao.Logic
{
    public enum DelayServiceCondition
    {
        None = 0,
        /// <summary>
        /// 延时前计算，固定为真
        /// </summary>
        True = 1,
        /// <summary>
        /// 延时前计算固定为否
        /// </summary>
        False = 2,
        /// <summary>
        /// 执行时实时计算
        /// </summary>
        Realtime = 3
    }

    public class DelayService : BaseLevelEO
    {
        private DelayServiceCondition _conditionState;
        private bool _autoRelease;
        private int _execFrame;

        public override uint MaxStore => 50;
        public bool isExpired { get; set; }
        public bool needSave { get; private set; }
        public LevelServiceUnit serviceUnit { get; private set; }
        public LevelPlayer player { get; private set; }
        public LevelEntity entity => serviceUnit.host as LevelEntity;
        public LevelRegion region => serviceUnit.host?.region ?? player.region;

        public void Init(int delay, LevelPlayer player, LevelServiceUnit serviceUnit, DelayServiceCondition conditionState, bool autoRelease, bool needSave)
        {
            this.serviceUnit = serviceUnit;
            this.player = player;
            _conditionState = conditionState;
            _autoRelease = autoRelease;
            _execFrame = region.level.levelFrame + delay;
            this.needSave = needSave;
        }

        public void LogicOnUpdate()
        {
            if (isExpired)
                return;
            if (region.level.levelFrame > _execFrame)
            {
                bool isConditionOK;
                switch (_conditionState)
                {
                    case DelayServiceCondition.True:
                        isConditionOK = true;
                        break;
                    case DelayServiceCondition.False:
                        isConditionOK = false;
                        break;
                    case DelayServiceCondition.Realtime:
                        isConditionOK = serviceUnit.CheckCondition(player);
                        break;
                    default:
                        isConditionOK = false;
                        break;
                }
                serviceUnit.Execute(player, isConditionOK);
                isExpired = true;
            }
        }

        public override IPOD GeneratePOD()
        {
            DelayServicePOD pod = new DelayServicePOD();
            pod.serviceCid = serviceUnit.serviceId;
            pod.unitIndex = serviceUnit.index;
            pod.regionCid = region.CfgRegionInstance.Id;
            pod.playerId = player.id;
            pod.entityId = entity == null ? 0 : entity.id;
            pod.leftFrame = _execFrame - region.level.levelFrame;
            pod.conditionState = (int)_conditionState;
            return pod;
        }

        public override void OnReset()
        {
            if (_autoRelease)
            {
                ClassPool.Put(serviceUnit);
            }
            serviceUnit = null;
            _autoRelease = false;

            player = null;
            isExpired = false;
            _conditionState = DelayServiceCondition.None;
            _execFrame = 0;
            needSave = false;
        }
    }
}
